package com.lagou.dao.impl;


import com.lagou.dao.CourseLessonDao;
import com.lagou.pojo.Course_Lesson;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

/**
 * @author cyt
 * @date 2020/8/29
 */
public class CourseLessonDaoImpl implements CourseLessonDao {
    @Override
    public int saveLesson(Course_Lesson lesson) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "INSERT INTO  course_lesson(\n" +
                    "    course_id,\n" +
                    "    section_id,\n" +
                    "    theme,\n" +
                    "    duration,\n" +
                    "    is_free,\n" +
                    "    order_num,\n" +
                    "    create_time,\n "+
                    "    update_time\n"+
                    ")VALUES(?,?,?,?,?,?,?,?);";

            Object[] params = {lesson.getCourse_id(),lesson.getSection_id(),
            lesson.getTheme(),lesson.getDuration(),lesson.getIs_free(),
            lesson.getOrderNum(),lesson.getCreate_time(), lesson.getUpdate_time()};

            int update = qr.update(sql, params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }

    @Override
    public int updateLesson(Course_Lesson lesson) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "UPDATE course_lesson SET \n" +
                    "course_id = ?,\n" +
                    "section_id= ?,\n" +
                    "theme= ?,\n" +
                    "duration= ?,\n" +
                    "is_free= ?,\n" +
                    "order_num= ?,\n" +
                    "update_time = ?\n"+
                    "WHERE id = ?;";

            Object[] params = {lesson.getCourse_id(),lesson.getSection_id(),
                    lesson.getTheme(),lesson.getDuration(),lesson.getIs_free(),
                    lesson.getOrderNum(), lesson.getUpdate_time(),lesson.getId()};

            int update = qr.update(sql, params);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return 0;
        }
    }
}
